﻿/*************************************************************************
	Copyright (C) 2009-2010 欣风工作室    
	All rights reserved
	
	CLR版本:			2.0.50727.4952
	命名空间名称:		WindowsFormsApplication1
	文件名:				MyHtmlTools
	创建时间:			2011-01-23 00:47:23
	作者:				我烤
	QQ:					8230707
	E-Mail:				xfgzskof@163.com
	
	修改日志:
	
*************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using mshtml;

namespace 街头篮球自动购买道具
{
    public class MyHtmlTools
    {
        private WebBrowser objWeb;

        public MyHtmlTools() { }
        public MyHtmlTools(WebBrowser w)
        {
            this.objWeb = w;
        }

        /// <summary>
        /// 获取指定地址的页面HTML
        /// </summary>
        /// <param name="URL">网页地址</param>
        /// <returns>页面HTML内容</returns>
        public string GetHtml(string URL)
        {
            WebRequest wrt;
            wrt = WebRequest.Create(URL);
            wrt.Credentials = CredentialCache.DefaultCredentials;
            WebResponse wrp;
            wrp = wrt.GetResponse();
            return new StreamReader(wrp.GetResponseStream(), Encoding.Default).ReadToEnd();
        }

        /// <summary>
        /// 模拟点击连接
        /// </summary>
        /// <param name="URL">地址</param>
        public void HerfClick(WebBrowser webBr, string URL)
        {
            for (int i = 0; i < webBr.Document.All.Count; i++)
            {
                if (webBr.Document.All[i].TagName == "A" && webBr.Document.All[i].GetAttribute("href").ToString().Trim() == URL)
                {
                    webBr.Document.All[i].InvokeMember("click");//引发”CLICK”事件
                    break;
                }
            }
        }

        /// <summary>
        /// 通过元素ID获取指定框架页面元素
        /// </summary>
        /// <param name="frameName">框架名称</param>
        /// <param name="ID">元素ID</param>
        /// <returns>HtmlElement</returns>
        public HtmlElement GetElementByID(string frameName, string ID)
        {
            HtmlElement element;
            element = objWeb.Document.Window.Frames[frameName].Document.GetElementById(ID);

            return element;
        }
        /// <summary>
        /// 通过元素ID获取页面元素
        /// </summary>
        /// <param name="ID">元素ID</param>
        /// <returns>HtmlElement</returns>
        public HtmlElement GetElementByID(string ID)
        {
            HtmlElement element;
            element = objWeb.Document.GetElementById(ID);

            return element;
        }

        /// <summary>
        /// 通过元素name获取指定框架页面元素
        /// </summary>
        /// <param name="frameName">框架名称</param>
        /// <param name="name">元素name</param>
        /// <returns>HtmlElement</returns>
        public HtmlElement GetElementByName(string frameName, string name)
        {
            HtmlElement element;
            element = objWeb.Document.Window.Frames[frameName].Document.All[name];

            return element;
        }
        /// <summary>
        /// 通过元素name获取页面元素
        /// </summary>
        /// <param name="name">元素name</param>
        /// <returns>HtmlElement</returns>
        public HtmlElement GetElementByName(string name)
        {
            HtmlElement element;
            element = objWeb.Document.All[name];

            return element;
        }

        /// <summary>
        /// 通过页面元素Type获取元素，仅用于特殊情况，如页面只存在一个该类型的元素
        /// </summary>
        /// <param name="typeName">元素type</param>
        /// <returns>HtmlElement</returns>
        public HtmlElement GetElementByType(string tagName, string typeName)
        {
            HtmlElementCollection elmList = objWeb.Document.GetElementsByTagName(tagName);

            foreach (HtmlElement element in elmList)
            {
                if (element.GetAttribute("type").ToString().ToLower() == typeName.ToLower())
                {
                    return element;
                }
            }
            return null;
        }

        /// <summary>
        /// 通过表单name获取指定框架的表单
        /// </summary>
        /// <param name="frameName">框架名称</param>
        /// <param name="name">表单名称</param>
        /// <returns>HtmlElement</returns>
        public HtmlElement GetDocumentFormByName(string frameName, string name)
        {
            HtmlElement element;
            element = objWeb.Document.Window.Frames[frameName].Document.Forms[name];

            return element;
        }
        /// <summary>
        /// 通过表单name获取页面表单
        /// </summary>
        /// <param name="name">表单名称</param>
        /// <returns>HtmlElement</returns>
        public HtmlElement GetDocumentFormByName(string name)
        {
            HtmlElement element;
            element = objWeb.Document.Forms[name];

            return element;
        }

        /// <summary>
        /// 获取指定ID的图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Image GetRegCodePic(string id)
        {
            HTMLDocument doc = (HTMLDocument)objWeb.Document.DomDocument;
            HTMLBody body = (HTMLBody)doc.body;
            IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange();
            IHTMLControlElement Img = (IHTMLControlElement)GetElementByID(id).DomElement;

            rang.add(Img);
            rang.execCommand("Copy", false, null);
            Image RegImg = Clipboard.GetImage();
            Clipboard.Clear();
            return RegImg;
        }
    }
}
